package com.abe.libquick.base;

import android.app.AlertDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProvider;
import androidx.viewbinding.ViewBinding;

import com.abe.libcore.base.screen.j.AbeJActivity;

public abstract class BaseActivity<T extends ViewBinding> extends AbeJActivity {
    protected T binding;
    private AlertDialog loadDialog;

    protected abstract T getBinding();

    protected abstract void initContent();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = getBinding();
        setContentView(binding.getRoot());
        initContent();
    }

    @Override
    public Boolean isAutoZoom() {
        return false;
    }

    TextView loadTextView;

    public void showLoadDialog(String msg) {
        if (!isAlive) return;
        msg = TextUtils.isEmpty(msg) ? "Loading..." : msg;
        if (loadDialog == null) {
            LinearLayout layout = new LinearLayout(this);
            layout.setGravity(Gravity.CENTER);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.setPadding(0, 50, 0, 50);
            ProgressBar progressBar = new ProgressBar(this);
            progressBar.setIndeterminate(true);
            loadTextView = new TextView(this);
            loadTextView.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 20, 0, 0);
            layout.addView(progressBar);
            layout.addView(loadTextView, params);
            loadDialog = new AlertDialog.Builder(this)
                    .setView(layout)
                    .setCancelable(true).create();
        }
        loadTextView.setText(msg);
        if (!loadDialog.isShowing()) {
            loadDialog.show();
        }
    }

    public void disLoadDialog() {
        if (!isAlive) return;
        if (loadDialog != null) loadDialog.dismiss();
    }

    public static class VMFactory extends ViewModelProvider.AndroidViewModelFactory {
        public VMFactory() {
            super(ApplicationHelper.application());
        }
    }
}